package com.zt.bus.line.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zt.bus.line.inter.DriverMapper;
import com.zt.bus.line.model.Driver;
import com.zt.servlet.ReqBean;
import com.zt.system.model.BusinessException;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.DateUtil;
import com.zt.util.StringUtil;

import net.sf.json.JSONObject;

@Service
public class DriverService extends BaseService {

	@Autowired
	private DriverMapper driverMapper;

	/**
	 * 查询
	 *
	 * @param reqBean
	 * @return
	 */
	public JSONObject findDriver(ReqBean reqBean) {
		SqlBuilder sqlBuilder = new SqlBuilder("", new Object[0]);
		return this.queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
	}

	/**
	 * 新增
	 *
	 * @param reqBean
	 */
	public void addDriver(ReqBean reqBean) {
		Driver driver = getDriver(reqBean);
		driver.setMakeTime(driver.getModifyTime());

		driverMapper.insert(driver);
	}

	/**
	 * 修改
	 *
	 * @param reqBean
	 */
	public void updateDriver(ReqBean reqBean) {
		String driverNo = reqBean.getInfo("driverNo");
		if (StringUtil.isEmpty(driverNo)) {
			throw new BusinessException("获取主键失败");
		}
		Driver driver = getDriver(reqBean);
		driverMapper.updateByPrimaryKeySelective(driver);
	}

	/**
	 * 删除
	 *
	 * @param reqBean
	 */
	public void removeDriver(ReqBean reqBean) {
		String driverNo = reqBean.getInfo("driverNo");
		if (StringUtil.isEmpty(driverNo)) {
			throw new BusinessException("获取主键失败");
		}
		driverMapper.deleteByPrimaryKey(driverNo);
	}

	/**
	 * 从表单获取对象
	 *
	 * @param reqBean
	 * @return
	 */
	private Driver getDriver(ReqBean reqBean) {
		Driver driver = new Driver();

		driver.setDriverNo(reqBean.getInfo("driverNo"));
		driver.setDriverName(reqBean.getInfo("driverName"));
		driver.setDriverPhone(reqBean.getInfo("driverPhone"));
		driver.setOperator(reqBean.getUserId());
		driver.setMakeTime(DateUtil.parseDateTime(reqBean.getInfo("makeTime")));
		driver.setModifyTime(new Date());
		driver.setRemark(reqBean.getInfo("remark"));

		return driver;
	}

	/**
	 * 查询全部司机
	 * 
	 * @return
	 */
	public List<Driver> getAllDriver() {
		return driverMapper.selectAll();
	}

	/**
	 * 查询司机
	 * 
	 * @return
	 */
	public Driver getByDriverNo(String driverNo) {
		return driverMapper.selectByPrimaryKey(driverNo);
	}
}